<?php
namespace App\Controllers\Ticket;

use App\Libs\Helper\CommonHelper;
use App\Models\Activity\Activity;
use App\Models\Lession\CommentCopy;
use App\Models\Lession\Scolumn;
use App\Models\Lession\Lession;
use App\Libs\Views\Json;
use App\Models\Activity\ActivityOrder;
/**
        //根据条件：返回订单列表；
    必传
 * type：1：已付款，未使用的
 *      2：已使用的；
 *
 * 选传
 * page：1
 * size：10，
 * start_time:
 * end_time:
 * acid:指定活动
 */
class GetTicketList extends \App\Libs\Base\BaseAction
{

    public function execute()
    {

        $page = $this->request->getQuery('page','int',1);
        $size = $this->request->getQuery('size','int',10);
        $offset = ($page-1)*$size;
        $start = $this->request->getQuery('start_time')??'';
        $end = $this->request->getQuery('end_time')??'';
        if(!$this->request->hasQuery('type')){
            return Json::view(1,'指定type',[]);
        }
        $type = $this->request->getQuery('type');
        $acid = $this->request->getQuery('acid')??'';
        switch ($type){
            case '1'://已付款；未使用的
                $conditions = [
                   'conditions'=>"status=1 ",
                    'columns'=>'id,name,phone,acid,tnum,price,status',
                    'order' => 'ctime desc',
                    'offset' => $offset,
                    'limit' =>$size
                ];
                break;
            case '2'://已经使用的
                $conditions = [
                    'conditions' =>"status=2 ",
                    'columns'=>'id,name,phone,acid,tnum,price,use_time,status',
                    'order' => 'ctime desc',
                    'offset' => $offset,
                    'limit' =>$size
                ];
                break;
            case '3':
                $conditions = [
                    'conditions' =>"(status=2 or status=1) ",
                    'columns'=>'id,name,phone,acid,tnum,price,use_time,status',
                    'order' => 'ctime desc',
                    'offset' => $offset,
                    'limit' =>$size
                ];
                break;
            default:
                return Json::view(1,'查询类型错误',[]);
        }
        if(!empty($start) && !empty($end)){

            $start = strtotime($start);
            $end = strtotime($end);
            if(($end-$start) > 31*24*60*60){
                return Json::view(1,'时间范围过广，最大31天',[]);
            }
            $conditions['conditions'].=" and unix_timestamp(use_time)>$start and unix_timestamp(use_time)<$end";
        }
        if(!empty($acid)){
            $matchActivity = Activity::count($acid);
            if(empty($matchActivity)){
                return Json::view(1,'指定的活动不存在',[]);
            }
            $conditions['conditions'].=" and acid=$acid";
        }
        $matchAcOrder = ActivityOrder::findAll($conditions)->toArray();

        if(empty($matchAcOrder)){
            return Json::view(0,'没有更多数据了',[]);
        }

        foreach($matchAcOrder as $val){
            $acidArray[] = $val['acid'];
        }

        $allActi = Activity::findAll([
            'id in ({ddd:array})',
            'bind' => ['ddd'=>$acidArray],
            'columns' => 'id,title,price'
        ]);
        $transAllActi = CommonHelper::setColToKey($allActi,'id');

        foreach($matchAcOrder as $key => $val){
            if(isset($transAllActi[$matchAcOrder[$key]['acid']])){
                $matchAcOrder[$key]['activity_title'] = $transAllActi[$matchAcOrder[$key]['acid']]['title'];
                $matchAcOrder[$key]['ticket_price_each'] = $transAllActi[$matchAcOrder[$key]['acid']]['price'];
            }else{
                $matchAcOrder[$key]['activity_title'] = '未知';
                $matchAcOrder[$key]['ticket_price_each'] = '未知';
            }
        }

        $allNum = ActivityOrder::count($conditions['conditions']);

        return Json::view(0,'ok',['order'=>$matchAcOrder,'sum'=>$allNum]);
    }
}
